package com.cellulam.framework.mq.send;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.order.OrderProducer;
import com.cellulam.framework.core.base.ResourceId;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;

import java.time.LocalDateTime;
import java.util.Properties;

/**
 * 顺序消息发送
 */
@Slf4j
public class MqOrderedMessageSender
        extends BaseAbstractMessageSender<OrderProducer> {

    public MqOrderedMessageSender(String groupId, String topic) {
        super(groupId, topic);
    }

    @Override
    protected final OrderProducer createProducer(Properties properties) {
        return ONSFactory.createOrderProducer(properties);
    }

    @Override
    protected final SendResult send(Message message) {
        return this.getProducer().send(message, message.getShardingKey());
    }

    @Override
    public <T extends ResourceId> SendResult send(String tag, T body, long delayMillis) {
        throw new NotImplementedException(String.format("[%s]顺序消息不支持延迟发送, topic: %s, tag: %s",
                this.getGroupId(),
                this.getTopic(),
                tag));
    }

    @Override
    public <T extends ResourceId> SendResult send(String tag, T body, LocalDateTime startDeliverTime) {
        throw new NotImplementedException(String.format("[%s]顺序消息不支持定时发送, topic: %s, tag: %s",
                this.getGroupId(),
                this.getTopic(),
                tag));
    }
}
